package com.mach.platform.service;

import com.mach.platform.domain.BaseCode;
import com.mach.platform.domain.base.Permission;
import com.mach.platform.repository.BaseCodeRepo;
import com.mach.platform.utils.Utils;
import com.mach.platform.utils.springdata.DynamicSpecifications;
import com.mach.platform.utils.springdata.SearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @Description:
 * @Package: com.mach.platform.service
 * @time 2018/8/13 0013 15:28
 */
@Service
public class BaseCodeServiceImpl implements BaseService<BaseCode> {

	@Autowired
	private BaseCodeRepo baseCodeRepo;

	/**
	 * 保存
	 *
	 * @param baseCode 保存的实体
	 * @return T 返回保存后的实体
	 */
	@Transactional(rollbackOn = Exception.class)
	@Override
	public BaseCode save (BaseCode baseCode) {
		return baseCodeRepo.saveAndFlush(baseCode);
	}

	/**
	 * 插入，批量保存
	 *
	 * @param list 实体集合
	 * @return List<T>
	 */
	@Override
	public List<BaseCode> insertObjInBatch (List<BaseCode> list) {
		return null;
	}

	/**
	 * 更新
	 *
	 * @param baseCode 跟新的实体
	 * @return T
	 */
	@Transactional(rollbackOn = Exception.class)
	@Override
	public BaseCode update (BaseCode baseCode) {
		return baseCodeRepo.saveAndFlush(baseCode);
	}

	/**
	 * 删除--批量删除
	 *
	 * @param ids 批量删除id
	 * @return int 影响数
	 */
	@Transactional(rollbackOn = Exception.class)
	@Override
	public int delete (Long[] ids) {
		return baseCodeRepo.deleteByIdIn(ids);
	}

	/**
	 * 获取列表数据---使用 com.mach.platform.utils.springdata  下的DynamicSpecifications 的
	 * 写法进行分页查询
	 *
	 * @param pageable 分页参数
	 * @param params   其他参数
	 * @return Page<T>
	 */
	@Override
	public Page<BaseCode> list (Pageable pageable, Map params) {

		if (params.get("queryStr") != null && !params.get("queryStr").toString().equals("")) {
			String str = params.get("queryStr").toString();
			if (Utils.isNumeric(str)) {
				params.put("groupType_eq", Integer.parseInt(str));
			} else {
				params.put("name_remark_plike", str);
			}
			params.remove("queryStr");
		}

		Specification condications = DynamicSpecifications
				                             .bySearchFilter(SearchFilter.parse(params).values(), Permission.class);
		return baseCodeRepo.findAll(condications, pageable);
	}

	/**
	 * 获取单体 数据
	 *
	 * @param id 获取详情的id
	 * @return T
	 */
	@Override
	public BaseCode info (Long id) {
		return null;
	}

	public Map getOptions ( ) {

		List<BaseCode> dbResults = baseCodeRepo.findAll(Sort.by(new String[] { "groupType", "sort" }));

		if (dbResults.size() > 0) {
			Map<Integer, List<BaseCode>> groupResult = dbResults.stream()
			                                                    .collect(Collectors.groupingBy(BaseCode::getGroupType));

			return groupResult;
		} else {
			return Collections.emptyMap();
		}

	}
}
